<?php

/**
 * 腾讯地图API
 */
class MapApi
{

    protected $url = "https://apis.map.qq.com";

    protected $key;
    //终端ID
    protected $service_id;

    public function __construct()
    {
        $this->key = config("site")["map_key"];
        $this->service_id = config("site")["map_service_id"];
    }

    /**
     * 终端列表
     * @return mixed
     */
    public function list_terminal()
    {
        return $this->http_request($this->url."/tracks/entity/list",[
            "service_id"    => $this->service_id,
            "page_index"    => 1,
            "page_size"     => 200,
        ],"post");
    }

    /**
     * 终端自定义列表
     * @return mixed
     */
    public function list_field_terminal()
    {
        return $this->http_request($this->url."/tracks/entity_field/list",[
            "service_id"    => $this->service_id,
        ],"post");
    }
    /**
     * 终端创建自定义字段
     * @param $field
     * @param $type
     * @return void
     */
    public function create_field($field,$type)
    {
        return $this->http_request($this->url."/tracks/entity_field/create",[
            "service_id"    => $this->service_id,
            "entity_field"  => $field,
            "type"          => $type?$type:"string"
        ],"post");
    }

    /**
     * 终端删除自定义字段
     * @param $field
     * @return void
     */
    public function delete_field($field)
    {
        return $this->http_request($this->url."/tracks/entity_field/delete",[
            "service_id"    => $this->service_id,
            "entity_field"  => $field,
        ],"post");
    }

    /**
     * 创建终端
     * @param $entity_id string 终端唯一ID
     * @param $entity_son_id int 终端子ID
     * @param $entity_type string 终端类型:express=快递,order=订单
     * @param $title string 终端名称
     * @param $desc string 描述
     * @param int $status (快递员/订单状态):0=未操作,1=已操作
     * @return mixed
     */
    public function create_terminal(string $entity_id,int $entity_son_id,string $entity_type,string $title,string $desc="",int $status=0)
    {
        return $this->http_request($this->url."/tracks/entity/create",[
            "service_id"    => $this->service_id,
            "entity_id"     => $entity_id,
            "entity_name"   => $title,
            "entity_desc"   => $desc,
            "entity_fields" => [
                "entity_son_id" => $entity_son_id,
                "entity_type" => $entity_type,
                "status"      => $status,
                "type"        => 1,//(快递员工作状态):0=休息中,1=工作中
            ]
        ],"post");
    }

    /**
     * 修改终端
     * @param string $entity_id 终端ID
     * @param string $title 名称
     * @param string $desc 描述
     * @param int $status (订单状态):0=空闲中,1=进行中
     * @param int $type (快递员工作状态):0=休息中,1=工作中
     * @return mixed
     */
    public function update_terminal(string $entity_id,string $title,string $desc="",int $status = 0,int $type = 1)
    {
        $up_data = [
            "service_id"    => $this->service_id,
            "entity_id"     => $entity_id,
            "entity_name"   => $title,
            "entity_fields" => [
                "status"    => $status,//(订单状态):0=未操作,1=已操作
                "type"      => $type,//(快递员工作状态):0=休息中,1=工作中
            ]
        ];
        if($desc){
            $up_data["entity_desc"] = $desc;
        }
        return $this->http_request($this->url."/tracks/entity/update",$up_data,"post");
    }

    /**
     * 删除终端
     * @param string $entity_id
     */
    public function delete_terminal(string $entity_id)
    {
        return $this->http_request($this->url."/tracks/entity/delete",[
            "service_id"    => $this->service_id,
            "entity_id"     => $entity_id
        ],"post");
    }

    /**
     * 上传轨迹
     * @param string $entity_id
     * @param float $lat 纬度
     * @param float $lng 经度
     * @param int  $speed 速度
     * @param int $altitude 高度
     * @param int $accuracy 定位精度
     * @return mixed
     */
    public function create_track(string $entity_id,float $lat,float $lng,int $speed = 0,int $altitude = 0,int $accuracy = 30)
    {
        return $this->http_request($this->url."/tracks/trace/upload",[
            "service_id"    => $this->service_id,
            "entity_id"     => $entity_id,
            "coord_type"    => 5,
            "data"          => [
                [
                    "lat"      => $lat,
                    "lng"      => $lng,
                    "loctime"  => time(),
                    "speed"    => $speed,
                    "direction"=> 0,
                    "altitude" => $altitude,
                    "accuracy" => $accuracy,
                ]
            ],
        ],"post");
    }

    /**
     * 轨迹查询
     * @param string $entity_id
     * @param string $start_t 查询开始时间
     * @param string $end_t 查询结束时间
     * @return mixed
     */
    public function search_track(string $entity_id,string $start_t,string $end_t)
    {
        return $this->http_request($this->url."/tracks/trace/search",[
            "service_id"    => $this->service_id,
            "entity_id"     => $entity_id,
            "start_time"    => $start_t,
            "end_time"      => $end_t,
        ],"get");
    }

    /**
     * 终端搜索
     * @param $query_data
     * @param int $page_index
     * @param int $page_size
     * @return mixed
     */
    public function search_terminal($query_data,int $page_index=1, int $page_size=200)
    {
        $search_data = [
            "service_id"    => $this->service_id,
            "location"      => $query_data["location"],//经纬度
            "radius"        => $query_data["radius"],//半径
            "page_index"    => $page_index,
            "page_size"     => $page_size
        ];
        //终端类型:express=快递员,order=订单
        $entity_type = isset($query_data["entity_type"]) && $query_data["entity_type"]?$query_data["entity_type"]:"express";
        //快递员状态:0=工作中,1=休息中
        $type        = isset($query_data["type"]) && $query_data["type"] > 0 ?$query_data["type"]:0;
        $fields = sprintf("entity_fields.entity_type=%s  and entity_fields.type=%s ",$entity_type,$type);
        //子ID查询
        if(isset($query_data["entity_son_id"]) && $query_data["entity_son_id"]){
            $fields .= " and entity_fields.entity_son_id = ".$query_data["entity_son_id"];
        }
        //包裹状态
        if( $entity_type == "order"){
            $fields .= " and entity_fields.status = 0";
        }
        $search_data["filter"] = $fields;
        if(isset($query_data["orderby"])){
            $search_data["orderby"] = $query_data["orderby"];
        }
        return $this->http_request($this->url."/tracks/entity/search",$search_data,"get");
    }

    private  function http_request($url,$params,$method="get")
    {
        $curl = curl_init();
        $params["key"] = $this->key;
        if($method == "get"){
            $url.="?".http_build_query($params);
        }
        curl_setopt($curl,CURLOPT_URL,$url);
        curl_setopt($curl,CURLOPT_HEADER,false);
        curl_setopt($curl,CURLOPT_HTTPHEADER,[
            "Content-Type:application/json",
        ]);
        curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
        if($method == "post"){
            curl_setopt($curl,CURLOPT_POST,1);
            curl_setopt($curl,CURLOPT_POSTFIELDS,json_encode($params));
        }
        $data = curl_exec($curl);
        curl_close($curl);
        return json_decode($data,true);
    }
}